//第一步解析地址
var googleMapData = [];
//如果详细地址未匹配到信息则使用列表中的数据
function setAddressByList(res,googleMapData,address)
{
    if(res.data.geocodes.length < 1 || "OK" != res.data.info){
        if(googleMapData.length > 0){
            $.each(googleMapData,function (i,v) {
                if(v.formatted_address==address){
                    $.ajaxSettings.async = false;
                    var location = v.location.split(',');
                    $.post('/api/common/getAmapImgByLatLng',{
                        lat:location[1],
                        lng:location[0],
                        zoom:8
                    },function (img_res) {
                        if(img_res.code>0){
                            res.data.google_map_img = img_res.data.google_map_img;
                            res.data.info = 'OK';
                        }
                    },'json')
                    $.ajaxSettings.async = true;
                    res.data.geocodes.push(v);return false;
                }
            })
        }
    }

    return res;
}
function setAddress(address,address_input,address_format_input) {
    console.log(address,address_input);
    var load_address = layer.load(2);
    //切换成接口访问
    $.post('/api/common/getAmapAddressList',{address:address,getMap:true},function (res) {
        if(res.code>0){
            var res = setAddressByList(res,googleMapData,address);
            console.log(res)
            var candidates = res.data.geocodes;
            if(candidates.length >0 && "OK" == res.data.info){
                //address_input.val(address);
                var location = candidates[0].location.split(',');
                $("#newpoi input[name=lat]").val(location[1]);
                $("#newpoi input[name=lng]").val(location[0]);
                $("#newpoi input[name=place_id]").val(candidates[0].adcode);
                $("#newpoi input[name=eng_name]").val('');
                $("#newpoi input[name=address]").val(address);
                $("#newpoi input[name=map_thumb_img]").val(res.data.google_map_img);
                $("#newpoimap img").attr('src',res.data.google_map_img);
            }
        }
        layer.close(load_address);
    },'json')
}
//用地址结果请求继续解析地址
function getAddressList(_that) {
    var address = _that.val();
    var addressbox="";
    if(address=='' || address==undefined){
        return false;
    }
    //切换成接口访问
    $.post('/api/common/getAmapAddressList',{address:address},function (res) {
        console.log(res);
        if(res.code>0){
            var candidates = res.data.geocodes;
            googleMapData = res.data.geocodes;
            console.log(candidates);
            if(candidates.length > 0 && "OK" == res.data.info){
                $.each(candidates,function (i,v) {
                    if(i>4){
                        return false;
                    }
                    addressbox+="<span>"+v.formatted_address+"</span>";
                })
                console.log(addressbox);
                _that.next().html(addressbox);
            }
        }
    },'json')
}
//传入谷歌地址的数据结构
function formattedAddressToOrder(address) {
    var formatt = {
        "arbitrary":{
            "geocoded":{
                "name":address.name,
                "addr":address.formatted_address,
                "lat":address.geometry.location.lat,
                "lng":address.geometry.location.lng,
                "gid":address.place_id,
            }
        }
    };
    return JSON.stringify(formatt);
}
